package nqueens.core;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import nqueens.algorithm.ObjectiveCalculator;

/**
 * Represents a set of Chromosomes, each one is a possible solution 
 * for N-Queens problem.
 * 
 * @author Artur Alves
 * @author Danilo Torres
 * @author Rodrigo Bruno
 */
public class Population {
	
	private List<Chromosome> individuals;
	
	public Population() {
		individuals = new LinkedList<Chromosome>();
	}
	
	public void add(Chromosome c) {
		individuals.add(c);
	}
	
	public String toString() {
		// TODO: refazer toString
		return individuals.toString();
	}
	
	public Iterator<Chromosome> iterator() {
		return individuals.iterator();
	}

	public List<Chromosome> getIndividuals() {
		return individuals;
	}

	public void setIndividuals(List<Chromosome> individuals) {
		this.individuals = individuals;
	}

	public double meanObjective() {
		double mean = 0.0;
		for (Chromosome c : individuals) {
			mean += new ObjectiveCalculator().calcObjective(c);
		}
		return mean / individuals.size();
	}
	
	public double maxObjective() {
		double max = -1.0;
		for (Chromosome c : individuals) {
			max = Math.max(max, new ObjectiveCalculator().calcObjective(c));
		}
		return max;
	}

}
